/*
 * @功能描述: 
 * @版本: v1.0.0
 * @作者: LuAn
 * @Date: 2021-09-01 06:47:42
 * @LastEditTime: 2021-09-01 06:55:29
 */
#include "bsp_epit.h"

void epit_init(EPIT_Type *epit, uint32_t frac, int countdown_val)
{
    EPITx_CR_t* cr = (EPITx_CR_t*)&epit->CR;
    cr->full = 0;
    cr->ENMOD = 1;
    cr->OCIEN = 1;
    cr->RLD = 1; // set and froget mode
    cr->PRESCALAR = frac; // 分频系数
    cr->IOVW = 1; // 自动重置counter...
    cr->CLKSRC = 1; //选择     ipg_clk(66MHz)

    epit->CNR = countdown_val;
    epit->LR = countdown_val;
    epit->CMPR = 0; // 比较器值0
}

void epit_enable(EPIT_Type *epit, IRQn_Type nirq, system_irq_handler_t handle, void *args)
{
    system_register_irqhandler(nirq, handle, args);
    GIC_EnableIRQ(nirq);
    EPITx_CR_t* cr = (EPITx_CR_t*)&epit->CR;
    cr->EN = 1;
}
void epit_stop(EPIT_Type* epit)
{
    EPITx_CR_t* cr = (EPITx_CR_t*)&epit->CR;
    cr->EN = 0;
}
void epit_restart(EPIT_Type* epit, uint32_t count)
{
    epit_stop(epit); // 先停止
    EPITx_CR_t* cr = (EPITx_CR_t*)&epit->CR;
    epit->CNR = count;
    cr->EN = 1; // 重置
}
void epit_clr_isr(EPIT_Type* epit)
{
    epit->SR = 1;
}
